package com.gs.djys.main;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.utils.ViewPortHandler;
import com.gs.djys.Base.BaseBean;
import com.gs.djys.Base.MyAppliCation;
import com.gs.djys.Decision.decisionlist.DecisionListActivity;
import com.gs.djys.Opinion.opinion.OpinionActivity;
import com.gs.djys.R;
import com.gs.djys.Util.ToastUtils;
import com.gs.djys.adapter.LGRecycleViewAdapter;
import com.gs.djys.adapter.LGViewHolder;
import com.gs.djys.check.checksearch.CheckSearchActivity;
import com.gs.djys.inquest.inquestlist.InquestActivity;
import com.gs.djys.land.LandActivity;
import com.gs.djys.mvp.MVPBaseActivity;
import com.gs.djys.sign.SignActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;


/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class MainActivity extends MVPBaseActivity<MainContract.View, MainPresenter> implements MainContract.View {


    @Bind(R.id.left_te)
    TextView leftTe;
    @Bind(R.id.title_text)
    TextView titleText;
    @Bind(R.id.right_te)
    TextView rightTe;
    @Bind(R.id.img_icon)
    ImageView imgIcon;
    @Bind(R.id.name)
    TextView name;
    @Bind(R.id.recycle)
    RecyclerView recycle;
    @Bind(R.id.tc)
    TextView tc;
    @Bind(R.id.tj)
    TextView tj;
    @Bind(R.id.view_pager)
    ViewPager viewPager;

    private List<String> Datas = new ArrayList<>();
    private LGRecycleViewAdapter<String> adapter;
    private CommNotificationDialog Dialogs;
    private MOneFragment mOneFragment = new MOneFragment();
    private MTwoFragment mTwoFragment = new MTwoFragment();
    private MThreeFragment mThreeFragment = new MThreeFragment();
    private MFourFragment mFourFragment = new MFourFragment();
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    public void onRequestError(String msg) {
        ToastUtils.showShortToast(msg);
    }

    @Override
    protected int getLayout() {
        return R.layout.act_main;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        setadapter();
    }

    private void initData() {
        fragments.add(mOneFragment);
        fragments.add(mTwoFragment);
        fragments.add(mThreeFragment);
        fragments.add(mFourFragment);
        MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(myFragmentPagerAdapter);
        leftTe.setText(MyAppliCation.spUtils.getString("userName"));
        Datas.add("许可查验");
        mPresenter.getSnRoles(MyAppliCation.spUtils.getString("userCode"));
        rightTe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Dialogs == null) {
                    Dialogs = new CommNotificationDialog(getContext(), "", "是否退出？", "确认", "取消", new CommBtnListener() {
                        @Override
                        public void CommOkBtnClick() {
                            MyAppliCation.spUtils.clear();
                            gotoActivity(LandActivity.class, true);
                            Dialogs.close();
                        }

                        @Override
                        public void CommCancelBtnClick() {

                        }
                    });
                }
                Dialogs.show();
            }
        });
    }

    private void setadapter() {
        GridLayoutManager manager = new GridLayoutManager(MainActivity.this, 2);
        manager.setSmoothScrollbarEnabled(true);
        manager.setAutoMeasureEnabled(true);
        recycle.setLayoutManager(manager);
        recycle.setHasFixedSize(true);
        recycle.setNestedScrollingEnabled(false);
        adapter = new LGRecycleViewAdapter<String>(Datas) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_main;
            }

            @Override
            public void convert(LGViewHolder holder, String zCDBean, int position) {
                if (zCDBean.equals("许可查验")) {
                    holder.setImageResurce(R.id.im, R.drawable.xkcy_img);
                } else if (zCDBean.equals("勘验")) {
                    holder.setImageResurce(R.id.im, R.drawable.ky_img);
                } else if (zCDBean.equals("签字")) {
                    holder.setImageResurce(R.id.im, R.drawable.qz_img);
                } else if (zCDBean.equals("决定发证")) {
                    holder.setImageResurce(R.id.im, R.drawable.jdfz_img);
                } else if (zCDBean.equals("征求意见")) {
                    holder.setImageResurce(R.id.im, R.drawable.zqyj_img);
                }
                holder.setText(R.id.te, Datas.get(position));
            }
        };
        recycle.setAdapter(adapter);
        adapter.setOnItemClickListener(R.id.item_layout, new LGRecycleViewAdapter.ItemClickListener() {
            @Override
            public void onItemClicked(View view, int position) {
                if (Datas.get(position).equals("许可查验")) {
                    Intent intent = new Intent(MainActivity.this, CheckSearchActivity.class);
                    startActivity(intent);
                } else if (Datas.get(position).equals("勘验")) {
                    Intent intent = new Intent(MainActivity.this, InquestActivity.class);
                    startActivity(intent);
                } else if (Datas.get(position).equals("签字")) {
                    Intent intent = new Intent(MainActivity.this, SignActivity.class);
                    startActivity(intent);
                } else if (Datas.get(position).equals("决定发证")) {
                    Intent intent = new Intent(MainActivity.this, DecisionListActivity.class);
                    startActivity(intent);
                } else if (Datas.get(position).equals("征求意见")) {
                    Intent intent = new Intent(MainActivity.this, OpinionActivity.class);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void getUserCodes(BaseBean landBean) {
        if (landBean.getCode().equals("true")) {
            Datas.add("征求意见");
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void getSnRoless(BaseBean landBean) {
        if (landBean.getRoleIdKs() != null) {
            String[] strArr = landBean.getRoleIdKs().split(",");
            if (strArr != null && strArr.length > 0) {
                for (int i = 0; i < strArr.length; i++) {
                    if (strArr[i].equals("3")) {
                        Datas.add("勘验");
                    } else if (strArr[i].equals("4")) {
                        Datas.add("签字");
                    } else if (strArr[i].equals("7")) {
                        Datas.add("决定发证");
                    }
                }
            }
        }
        if (landBean.getRoleIdSn() != null) {
            String[] strArr2 = landBean.getRoleIdSn().split(",");
            if (strArr2 != null && strArr2.length > 0) {
                for (int i = 0; i < strArr2.length; i++) {
                    if (strArr2[i].equals("'3'")) {
                        Datas.add("勘验");
                    } else if (strArr2[i].equals("'4'")) {
                        Datas.add("签字");
                    }
                }
            }
            for (int i = 0; i < Datas.size() - 1; i++) {
                for (int j = Datas.size() - 1; j > i; j--) {
                    if (Datas.get(j).equals(Datas.get(i))) {
                        Datas.remove(j);
                    }
                }
            }
        }
        mPresenter.getUserCode(MyAppliCation.spUtils.getString("userCode"));
    }

}